package com.moke.module
{
	import com.moke.loader.ContentInfo;
	import com.moke.loader.ILoadingBar;
	import com.moke.loader.LoadType;
	import com.moke.loader.UILoader;
	
	import flash.events.EventDispatcher;
	import flash.events.ProgressEvent;
	
	/**
	 * 模块代理
	 * @author fraser
	 */ 
	public class ModuleProxy extends EventDispatcher implements IModuleable
	{
		private var _status:String = ModuleStatus.HIDE ;
		private var _url:String ;
		private var _title:String ;
		private var _loadingBar:ILoadingBar ;
		
		private var _module:BaseModule ;
		
		public function ModuleProxy(url:String,title:String,loadingBar:ILoadingBar)
		{
			super(this) ;
			_url = url ;
			_title = title ;
			_loadingBar = loadingBar ;
		}
		
		public function get status():String
		{
			return _status;
		}

		public function get url():String
		{
			return _url;
		}

		public function get title():String
		{
			return _title;
		}
		
		public function get module():BaseModule
		{
			return _module ;
		}
		
		// #######################  com.zmsg.core.module.IModuleable #############
		
		public function get lifecycleType():String
		{
			if(_module)
			{
				return _module.lifecycleType ;
			}
			return ModuleLifecycleType.CURRENT ;
		}

		private var _initData:Object ;
		
		public function init(data:Object):void
		{
			_initData = data ;
			if(_module)
			{
				_module.init(_initData) ;
			}
			setup() ;
		}

		public function show():void
		{
			_status = ModuleStatus.SHOW ;
			if(_module && _module.parent == null)
			{
				_module.show() ;
			}
			setup() ;
		}

		public function hide():void
		{
			_status = ModuleStatus.HIDE ;
			if(_module && _module.parent != null)
			{
				_module.hide() ;
			}
			_initData = null ;
		}

		public function dispose():void
		{
			ModuleManager.removeModuleProxy(_url) ;
			UILoader.cancel(onComplete) ;
			if(_module)
			{
				_module.dispose() ;
			}
			_initData = null ;
		}
		
		private var _isLoading:Boolean = false ; // 标示是否在加载中
		
		public function setup():void
		{
			if(_module || _isLoading){
				return ;
			}
			_isLoading = true ;
			UILoader.load(_url,LoadType.SWF,onComplete,onError,"",null,onOpen,onProgress) ;
		}
		
		private function onOpen(info:ContentInfo):void
		{
			if(_loadingBar != null)
			{
				_loadingBar.show(_title);
			}	
		}
		
		private function onProgress(event:ProgressEvent):void{			
			if(_loadingBar != null)
			{
				_loadingBar.progress(event.bytesLoaded / event.bytesTotal);
			}	
		}
		
		private function onError(info:ContentInfo):void
		{
			if(_loadingBar != null)
			{
				_loadingBar.hide() ;
			}
			_isLoading = false ;
			dispose() ;
		}
		
		private function onComplete(info:ContentInfo):void
		{
			if(_loadingBar != null)
			{
				_loadingBar.hide() ;
			}
			_isLoading = false ;
			_module = info.content as BaseModule ;
			_module.url = _url ;
			_module.setup() ;
			_module.init(_initData) ;
			if(_status == ModuleStatus.SHOW)
			{
				_module.show() ;
			}
		}
	}
}